<template>
  <div id="performance">
    <el-breadcrumb>
      <el-breadcrumb-item>个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>我的绩效</el-breadcrumb-item>
    </el-breadcrumb>
    <div id="main">
      <el-form :inline="true" :model="searchData" class="demo-form-inline">
        <el-form-item label="考核周期">
          <el-input v-model="searchData.nper"></el-input>
        </el-form-item>
        <el-form-item label="当前环节">
          <el-select v-model="searchData.link" placeholder="请选择">
            <el-option label="已完成" value="已完成"></el-option>
            <el-option label="未完成" value="未完成"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <div id="line"></div>

      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="num" label="流水号" width="180px">
        </el-table-column>
        <el-table-column prop="nper" label="考核周期" width="180px">
        </el-table-column>
        <el-table-column prop="link" label="当前环节" width="180px">
        </el-table-column>
        <el-table-column prop="name" label="审批人" width="180px">
        </el-table-column>
        <el-table-column prop="score" label="综合分数" width="120px">
        </el-table-column>
        <el-table-column prop="int_ime" label="时间"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              @click="performancefn(scope.row)"
              size="small"
              >填写绩效</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block block2">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="count"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalpage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      searchData: {
        nper: "",
        link: "",
      },
      searchData2: [],
      totaldata: [],
      currentPage4: 1,
      count: 5,
      mypage: "",
    };
  },
  methods: {
    onSubmit() {
      this.currentPage4 = 1;
      if (this.searchData.nper || this.searchData.link) {
        let nper = this.searchData.nper ? this.searchData.nper : "";
        let link = this.searchData.link ? this.searchData.link : "";
        this.page = 1;
        this.searchData2 = [nper, link];
      } else {
        this.searchData = "";
      }
    },
    reset() {
      this.currentPage4 = 1;
      this.show();
      this.totaldata = [];
      this.searchData = {};
      this.searchData2 = [];
    },
    handleSizeChange(n) {
      this.count = n;
    },
    handleCurrentChange(n) {
      this.currentPage4 = n;
    },
    performancefn: function (n) {
      console.log(n);
      if (n.state == "启用") {
        this.$store.commit("changeperid", n);
        if (n.link == "未完成") {
          this.$router.push("/home/performance");
        } else {
          this.open3("该绩效已填写完成");
        }
      } else {
        this.open3("该绩效未启用");
      }
    },
    open3(msg) {
      this.$message({
        message: msg,
        type: "warning",
      });
    },
    show() {
      console.log(this.$store.state.data);
      this.$axios({
        url: "/personalcenter/performance/get",
        params: { mysuper: JSON.parse(sessionStorage.getItem("user")).jobs },
      }).then((res) => {
        let x = [];
        res.data.data.forEach((v, i) => {
          v.score = v.score == null ? "未打分" : v.score;
          x.push(v);
        });
        this.totaldata = x;
      });
    },
  },
  created() {
    this.show();
  },
  computed: {
    totalpage: function () {
      if (this.searchData2.length != 0) {
        return this.mypage;
      } else {
        return this.totaldata.length;
      }
    },
    tableData: function () {
      if (this.searchData2.length != 0) {
        this.mypage = 0;
        let arr = [];
        for (let i = 0; i < this.totaldata.length; i++) {
          if (
            this.totaldata[i].nper == this.searchData2[0] &&
            this.totaldata[i].link == this.searchData2[1]
          ) {
            arr.push(this.totaldata[i]);
            console.log(this.totaldata[i].nper, this.totaldata[i].link);
          } else if (
            this.totaldata[i].link == this.searchData2[1] &&
            this.searchData2[0] == ""
          ) {
            arr.push(this.totaldata[i]);
          } else if (
            this.totaldata[i].nper == this.searchData2[0] &&
            this.searchData2[1] == ""
          ) {
            arr.push(this.totaldata[i]);
            console.log(this.searchData2[1]);
          }
        }
        this.mypage = arr.length;
        return arr.slice(
          (this.currentPage4 - 1) * this.count,
          this.currentPage4 * this.count
        );
      } else {
        return this.totaldata.slice(
          (this.currentPage4 - 1) * this.count,
          this.currentPage4 * this.count
        );
      }
    },
  },
};
</script>
<style lang="less" scoped>
#mymain {
  margin-top: 20px;
}
#main {
  margin-top: 20px;
  height: 70%;
  background-color: white;
  height: 100%;
  padding: 20px;
}
.el-menu {
  padding-left: 20px;
}
.el-form-item {
  margin-right: 20px;
}
#line {
  width: 100%;
  height: 0.1px;
  background-color: #e5e5e5;
  margin-bottom: 20px;
}
.block2 {
  margin-top: 30px;
  text-align: right;
}
</style>
